﻿using BookmarkingSite.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using System.Xml;


namespace BookmarkingSite.Client
{
    public static class XmlPersister
    {
        public static void ReadXmlFile(string pathToFile)
        {
            TransactionScope transaction = new TransactionScope(TransactionScopeOption.Required,
                    new TransactionOptions { IsolationLevel = IsolationLevel.RepeatableRead });
            using (transaction)
            {
                XmlTextReader reader = new XmlTextReader(pathToFile);

                using (reader)
                {
                    while (reader.Read())
                    {
                        string username = null;
                        string title = null;
                        string url = null;
                        string notes = null;
                        string tags = null;
                        string[] tagArray = { };
                        bool read = false;

                        while (reader.NodeType == XmlNodeType.Element && reader.Name != "bookmark" && reader.Name != "bookmarks")
                        {
                            read = true;
                            if (reader.Name == "username")
                            {
                                username = reader.ReadInnerXml();
                            }
                            else if (reader.Name == "title")
                            {
                                title = reader.ReadInnerXml();
                            }
                            else if (reader.Name == "url")
                            {
                                url = reader.ReadInnerXml();
                            }
                            else if (reader.Name == "notes")
                            {
                                notes = reader.ReadInnerXml();
                            }
                            else if (reader.Name == "tags")
                            {
                                tags = reader.ReadInnerXml();
                                tagArray = tags.Split(',');
                                for (int i = 0; i < tagArray.Length; i++)
                                {
                                    tagArray[i] = tagArray[i].Trim();
                                }
                            }

                            reader.Read();
                        }

                        if (username != null && title != null && url != null && read == true)
                        {
                            DatabasePersister.ImportBookmarks(username, title, url, notes, tagArray);
                        }
                        else if (read == true)
                        {
                            throw new InvalidOperationException("Username or title is incorrect.");
                        }
                    }
                }

                transaction.Complete();
            }
        }

        public static void ReadQueryFromXml(string pathToFile)
        {
            XmlReader reader = new XmlTextReader(pathToFile);
            using (reader)
            {
                while (reader.Read())
                {
                    string username = null;
                    string tagName = null;

                    while (reader.NodeType == XmlNodeType.Element && reader.Name != "query")
                    {
                        if (reader.Name == "username")
                        {
                            username = reader.ReadInnerXml();
                            reader.Read();
                        }
                        else if (reader.Name == "tag")
                        {
                            tagName = reader.ReadInnerXml();
                            reader.Read();
                        }
                    }

                    if (tagName != null)
                    {
                        DatabasePersister.SearchUsers(username, tagName);
                    }
                }
            }
        }        
    }
}
